/************************************************************************************/
/*  Program name CAPFNDIMP_VALUE_SET.sql                                            */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

ALTER TRIGGER FND_FLEX_VALUE_SETS_CT1 DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFNDIMP_VALUE_SET';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FND_VALUE_SETS;

	CURSOR Cur_G IS
	SELECT
		A.*,
		B.Flex_Value_Set_ID_N
	FROM
		CFND_FLEX_VALUE_SETS		A,
		CRST_FND_VALUE_SETS_MAP		B
	WHERE	A.Flex_Value_Set_ID	=	B.Flex_Value_Set_ID;

	lv_Next_Seq_ID	NUMBER;

BEGIN

	--  RENAME
	UPDATE
		FND_FLEX_VALUE_SETS
	SET
		Flex_Value_Set_Name	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(Flex_Value_Set_Name,1,4),Flex_Value_Set_Name)
	WHERE	Flex_Value_Set_ID	IN	(
									SELECT
										Flex_Value_Set_ID
									FROM
										CRST_FND_VALUE_SETS
									);

	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	FND_FLEX_VALUE_SETS_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT INTO CRST_FND_VALUE_SETS_MAP VALUES
			(
			C1.FLEX_VALUE_SET_ID,
			CAPBRIMP_MAPPING.RenameStr(SUBSTR(C1.FLEX_VALUE_SET_NAME,1,4),C1.FLEX_VALUE_SET_NAME),
			lv_Next_Seq_ID,
			C1.FLEX_VALUE_SET_NAME
			);

			INSERT INTO FND_FLEX_VALUE_SETS
			VALUES
			(
			lv_Next_Seq_ID,											--FLEX_VALUE_SET_ID,
			C1.FLEX_VALUE_SET_NAME,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),		--LAST_UPDATED_BY,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),			--CREATED_BY,
			C1.LAST_UPDATE_LOGIN,
			C1.VALIDATION_TYPE,
			C1.PROTECTED_FLAG,
			C1.SECURITY_ENABLED_FLAG,
			C1.LONGLIST_FLAG,
			C1.FORMAT_TYPE,
			C1.MAXIMUM_SIZE,
			C1.ALPHANUMERIC_ALLOWED_FLAG,
			C1.UPPERCASE_ONLY_FLAG,
			C1.NUMERIC_MODE_ENABLED_FLAG,
			C1.DESCRIPTION,
			C1.DEPENDANT_DEFAULT_VALUE,
			C1.DEPENDANT_DEFAULT_MEANING,
			C1.PARENT_FLEX_VALUE_SET_ID,
			C1.MINIMUM_VALUE,
			C1.MAXIMUM_VALUE,
			C1.NUMBER_PRECISION
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	BEGIN
		UPDATE	FND_FLEX_VALUE_SETS
		SET
			PARENT_FLEX_VALUE_SET_ID	=	CAPBRIMP_MAPPING.GetFNDValueSetIDMapping(PARENT_FLEX_VALUE_SET_ID)
		WHERE	FLEX_VALUE_SET_ID	IN	(
										SELECT
											FLEX_VALUE_SET_ID_N
										FROM
											CRST_FND_VALUE_SETS_MAP
										);
	EXCEPTION
		WHEN OTHERS THEN
			DBMS_OUTPUT.Put_Line(SQLERRM);
			lv_Err_Count	:=	lv_Err_Count + 1;
	END;

	BEGIN
		FOR C2 IN Cur_G LOOP
			INSERT INTO CFND_FLEX_VALUE_SETS VALUES
			(
				C2.Flex_Value_Set_ID_N,
				CAPBRIMP_MAPPING.GetOrgMapping(C2.S_ORG_ID),
				CAPBRIMP_MAPPING.GetBGMapping(C2.S_BUSINESS_GROUP_ID)
			);
		END LOOP;
	EXCEPTION
		WHEN OTHERS THEN
			DBMS_OUTPUT.Put_Line(SQLERRM);
			lv_Err_Count	:=	lv_Err_Count + 1;
	END;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/

ALTER TRIGGER FND_FLEX_VALUE_SETS_CT1 ENABLE;
